package org.leaf.green.b2b

class Product {

    def productService

    String serialNumber//品号
    String name//品名
    String unit//单位
    Integer shelfLife//保质期
    Boolean isActive//是否启用
    Date dateCreated//创建日期
    Date lastUpdated//上次修改日期

    static hasMany = [categories : ProductCategory]

    static constraints = {
        serialNumber(maxSize:20, blank:false, nullable: false, unique:true)
        name(maxSize:60, blank:false, nullable:false)
        unit(maxSize:4, nullable:false, blank: false)
        shelfLife(nullable:false, min:0, max:100)
        isActive(nullable:true)
    }

    String toString() {
        return "$serialNumber-$name"
    }    

    static mapping = {
        table 'B2B_PRODUCT'
    }

    def afterInsert(){
        //新增产品时，初始化库存
        log.info("[domain=Product, method=afterInsert]")
        Inventory.withNewSession{
            productService.initInventory(this)
        }
    }
}
